package com.familyan.jenkins.configure;

import com.lotus.core.web.cookyjar.CookyjarValueArgumentResolver;
import com.lotus.core.web.filter.NoSessionFilter;
import com.lotus.core.web.filter.ResponseOutputBufferFilter;
import com.lotus.core.web.security.DefaultSecurityHandlerInterceptor;
import com.lotus.core.web.security.JSONSecurityExcetionResolver;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * created by shaowenchao
 */
@Configuration
public class WebSecurityConfigurer implements WebMvcConfigurer {

    @Bean
    public FilterRegistrationBean noSessionFilterBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new NoSessionFilter());
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean responseOutputBufferFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new ResponseOutputBufferFilter());
        filterRegistrationBean.setOrder(Integer.MIN_VALUE);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean corsFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        //1.添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();

        //放行哪些原始域
        config.addAllowedOrigin("*");
        //是否发送Cookie信息
        config.setAllowCredentials(true);
        //放行哪些原始域(请求方式)
        config.addAllowedMethod("*");
        //放行哪些原始域(头部信息)
        config.addAllowedHeader("*");
        config.setMaxAge(2 * 60 * 60L);

        //2.添加映射路径
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        filterRegistrationBean.setFilter(new CorsFilter(configSource));
        return filterRegistrationBean;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 登陆验证
//        DefaultSecurityHandlerInterceptor securityInterceptor = new DefaultSecurityHandlerInterceptor();
//        securityInterceptor.setClazz(LoginMember.class);
//        registry.addInterceptor(securityInterceptor);
    }

    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        resolvers.add(new JSONSecurityExcetionResolver());
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
//        CookyjarValueArgumentResolver<LoginMember> resolver = new CookyjarValueArgumentResolver<>();
//        resolver.setClazz(LoginMember.class);
//        resolvers.add(resolver);

    }
}
